package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.service.INoteContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 * 
 * @author dh
 * @date 2025-09-16
 */
@RestController
@RequestMapping("notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private INoteContentService contentService;

    @Autowired
    private RemoteNoteEsService esService;

    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @PostMapping("/viewnumIncr")
    public R<Map<String,Object>> viewnumIncr(Long noteId){
        Map<String,Object> map =  noteService.viewnumIncr(noteId);
        return R.ok(map);
    }

    @GetMapping("/isUserFavor")
    public R<Boolean> isUserFavor(Long uid,Long noteId){
        return R.ok(noteService.isUserFavor(uid,noteId));
    }

    //添加收藏
    @RequiresLogin
    @PostMapping("/collect/{id}")
    public R<Map<String,Object>> collect(@PathVariable Long id){
        return R.ok(noteService.collect(id));
    }

    @RequiresLogin
    @PostMapping("/star/{id}")
    public R<Map<String,Object>> star(@PathVariable Long id){

        return R.ok(noteService.star(id));
    }


    @RequiresLogin
    @PostMapping("/add")
    public R<?> add(@RequestBody Note note)
    {
        noteService.add(note);
        return R.ok();
    }

    @GetMapping("/viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId){
        List<Note> list = noteService.viewnnumTop3(destId);
        return R.ok(list);
    }

    @GetMapping("/dataInit")
    public R<?> dataInit(){
        for (Note note : noteService.list()) {
            NoteEs es = new NoteEs();
            BeanUtils.copyProperties(note,es);
            esService.initNoteEs(es,SecurityConstants.INNER);
        }
        return R.ok();
    }

    /**
     * 旅游日记详情
     */
    @GetMapping("/detail/{id}")
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getById(id);
        //找到作者对象,查出作者头像
        UserInfo userInfo = remoteUserInfoService.getOne(note.getAuthorId(), SecurityConstants.INNER).getData();
        note.setAuthor(userInfo);
        //游记内容的id和游记的id一致
        NoteContent noteContent = contentService.getById(id);
        note.setContent(noteContent);
        return R.ok(note);
    }
    /**
     * 旅游日记 列表
     */
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }


    @InnerAuth
    @PostMapping("/feign/noteStatisHashPersistence")
    public R<Note>  noteStatisHashPersistence(){

        noteService.statisCondition();
        return R.ok();
    }

}
